using System;
using System.Collections;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;

using WHC.Pager.Entity;
using WHC.Hotel.Entity;
using WHC.Hotel.IDAL;
using Microsoft.Practices.EnterpriseLibrary.Data;
using WHC.OrderWater.Commons;

namespace WHC.Hotel.DAL
{
	/// <summary>
	/// DictType 的摘要说明。
	/// </summary>
	public class DictType : BaseDAL<DictTypeInfo>, IDictType
	{
		#region 对象实例及构造函数

		public static DictType Instance
		{
			get
			{
				return new DictType();
			}
		}
		public DictType() : base("tb_DictType","ID")
		{
            sortField = "Seq";
            IsDescending = false;
		}

		#endregion

		/// <summary>
		/// 将DataReader的属性值转化为实体类的属性值，返回实体类
		/// </summary>
		/// <param name="dr">有效的DataReader对象</param>
		/// <returns>实体类对象</returns>
		protected override DictTypeInfo DataReaderToEntity(IDataReader dataReader)
		{
			DictTypeInfo dictTypeInfo = new DictTypeInfo();
			SmartDataReader reader = new SmartDataReader(dataReader);
			
			dictTypeInfo.ID = reader.GetInt32("ID");
			dictTypeInfo.Name = reader.GetString("Name");
			dictTypeInfo.Remark = reader.GetString("Remark");
			dictTypeInfo.Seq = reader.GetString("Seq");
			dictTypeInfo.Editor = reader.GetInt32("Editor");
			dictTypeInfo.EditTime = reader.GetDateTime("EditTime");
            dictTypeInfo.Category = reader.GetInt32("Category");
			
			return dictTypeInfo;
		}
        
		/// <summary>
		/// 将实体对象的属性值转化为Hashtable对应的键值
		/// </summary>
		/// <param name="obj">有效的实体对象</param>
		/// <returns>包含键值映射的Hashtable</returns>
        protected override Hashtable GetHashByEntity(DictTypeInfo obj)
		{
		    DictTypeInfo info = obj as DictTypeInfo;
			Hashtable hash = new Hashtable(); 
			
 			hash.Add("Name", info.Name);
 			hash.Add("Remark", info.Remark);
 			hash.Add("Seq", info.Seq);
 			hash.Add("Editor", info.Editor);
 			hash.Add("EditTime", info.EditTime);
            hash.Add("Category", info.Category);
 				
			return hash;
		}

        /// <summary>
        /// 获取所有字典类型的列表集合(Key为名称，Value为ID值）
        /// </summary>
        /// <param name="dictTypeId">字典类型ID</param>
        /// <returns></returns>
        public Dictionary<string, string> GetAllType()
        {
            string sql = string.Format("select Name,ID from tb_DictType order by {0} {1}",
                sortField, IsDescending ? "DESC" : "ASC");

            Database db = DatabaseFactory.CreateDatabase();
            DbCommand command = db.GetSqlStringCommand(sql);

            Dictionary<string, string> list = new Dictionary<string, string>();
            using (IDataReader dr = db.ExecuteReader(command))
            {
                while (dr.Read())
                {
                    string name = dr["Name"].ToString();
                    string value = dr["ID"].ToString();
                    if (!list.ContainsKey(name))
                    {
                        list.Add(name, value);
                    }
                }
            }
            return list;
        }

        /// <summary>
        /// 获取所有字典类型的列表集合(Key为名称，Value为ID值）
        /// </summary>
        /// <param name="dictTypeId">字典类型ID</param>
        /// <returns></returns>
        public Dictionary<string, string> GetAllType(int category)
        {
            string sql = string.Format("select Name,ID from tb_DictType where category ={2} order by {0} {1}",
                sortField, IsDescending ? "DESC" : "ASC", category);

            Database db = DatabaseFactory.CreateDatabase();
            DbCommand command = db.GetSqlStringCommand(sql);

            Dictionary<string, string> list = new Dictionary<string, string>();
            using (IDataReader dr = db.ExecuteReader(command))
            {
                while (dr.Read())
                {
                    string name = dr["Name"].ToString();
                    string value = dr["ID"].ToString();
                    if (!list.ContainsKey(name))
                    {
                        list.Add(name, value);
                    }
                }
            }
            return list;
        }
    }
}